
#include <pic.h>
#include "eeprom.h"
#include "my_macros.h"
#include "Inputs.h"
#include "SysTime.h"

#define   PIN_HOOPER_ENABLE			RB3
#define   PIN_HOOPER_ENABLE_DIR		TRISB3

#define   PIN_HOOPER_ALARM				RB4
#define   PIN_HOOPER_ALARM_DIR		TRISB4

bit BankNote_enable;

unsigned char BankNoteNominal, Timeout_counter; //, _BankNoteNominal;

void Receiver_Init ( void )
{
	PIN_HOOPER_ENABLE = 0;
	PIN_HOOPER_ENABLE_DIR = OUTPUT;

	PIN_HOOPER_ALARM = 0;		
	PIN_HOOPER_ALARM_DIR = OUTPUT;

	BankNoteNominal = 0;

	BankNote_enable = TRUE;

	BankNoteNominal = EEPROM_READ(E2_ADDR_COUNTER );
	
	if ( BankNoteNominal == 0xff )
	{
		BankNoteNominal = 0;
	}
	
	//_BankNoteNominal = 0;
}

void Drive_Receiver ( void )
{
	if ( InputsEvent == INPUT_1 )
	{
		BankNoteNominal++;
	}


	if ( BankNote_enable && BankNoteNominal )
	{
		PIN_HOOPER_ENABLE = TRUE;
		
		if ( InputsEvent == INPUT_2 )
		{
			BankNoteNominal--;
			Timeout_counter = 0;
		}
	}
	else
	{
		PIN_HOOPER_ENABLE = FALSE;
	}

	if ( BankNoteNominal && Time_1000ms && ++Timeout_counter >= 5 )
	{
		BankNote_enable = FALSE;
		PIN_HOOPER_ALARM = 1;
	} 
}

/* bit TimeOut_Enable; */
/* unsigned char Counter;  */
/* unsigned int TimeOut_Counter; */

/* void Drive__Receiver ( void ) */
/* { */
	
/* 	if ( InputsEvent == INPUT_1 ) */
/* 	{ */
/* 		Counter++; */

/* 		TimeOut_Counter = 0; */
/* 		TimeOut_Enable = TRUE; */
/* 	} */

/* 	if ( TimeOut_Enable && Time_10ms && ++TimeOut_Counter > 50 ) */
/* 	{ */
/* 		TimeOut_Enable = FALSE; */
/* 		TimeOut_Counter = 0; */
		
		
/* 		BankNoteNominal = Counter; */
/* 		Counter = 0; */
/* 	}  */

	
/* 	if ( BankNoteNominal ) */
/* 	{ */
/* 		_BankNoteNominal += BankNoteNominal; */
/* 		BankNoteNominal = 0; */
/* 	} */

/* 	if ( _BankNoteNominal ) */
/* 	{ */

/* 		PIN_HOOPER_ENABLE = TRUE; */
		
/* 		if ( InputsEvent == INPUT_2 ) */
/* 		{ */
/* 			_BankNoteNominal--; */
/* 		} */
/* 	} */
/* 	else */
/* 	{ */
/* 		PIN_HOOPER_ENABLE = FALSE; */
/* 	} */
/* } */
